package org.easyway.tiles.generic;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;

public class GenericMapEditor extends JFrame {
	protected static final long serialVersionUID = -3848484232130165464L;

	protected String configFilename = "GMEConfig.cfg";

	protected JMenuBar menu = null;

	protected JMenu fileMenu = null;

	protected JMenu EditionMenu = null;

	protected JMenuItem openMenuItem = null;

	protected static JMenuItem saveMenuItem = null;

	protected JMenuItem exitMenuItem = null;

	protected JMenuItem newMenuItem = null;

	protected static String filename = ""; // @jve:decl-index=0:

	protected JPanel jPanel = null;

	protected GraphicBoard graphicBoard = null;

	protected JScrollPane jScrollPane = null;

	public static boolean isTool = true;

	protected JMenu editorMenu = null;

	protected JMenuItem addTextureMenuItem = null;

	protected JLabel selectedTile = null;

	protected JMenuItem addAllTextureMenuItem = null;

	protected JMenuItem RemoveAllTextureMenuItem = null;

	protected JScrollPane jScrollPaneTexture = null;

	public JPanel textureArea = null;

	protected JButton zoomIn = null;

	protected JButton ZoomOut = null;

	protected JMenuItem copyMenuItem = null;

	protected JMenuItem exportMenuItem = null;

	protected JMenuItem importMenuItem = null;

	protected JMenuItem pasteMenuItem = null;

	protected JButton selectCursor = null;

	protected JButton selectPaste = null;

	protected static Image image_copier;

	protected static Image image_paster;

	public static CurrentJPanel currentJPanel = null;

	protected JButton selectBlank = null;

	/**
	 * This method initializes
	 * 
	 */
	public GenericMapEditor() {
		super();
		TextureButton.gme = this;
		initialize();
		loadTextureButton();
		repaint();
	}

	/**
	 * This method initializes this
	 * 
	 */
	protected void initialize() {
		this.setMaximumSize(new Dimension(1024, 780));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(1024, 700);
		this.setContentPane(getJPanel());
		this.setJMenuBar(getMenu());
		this.setTitle("Tiles Map Editor");
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				close();
			}
		});
	}

	/**
	 * This method initializes menu
	 * 
	 * @return javax.swing.JMenuBar
	 */
	protected JMenuBar getMenu() {
		if (menu == null) {
			menu = new JMenuBar();
			menu.add(getFileMenu());
			menu.add(getEditionMenu());
			menu.add(getEditorMenu());
		}
		return menu;
	}

	/**
	 * This method initializes fileMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	protected JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getNewMenuItem());
			fileMenu.add(getOpenMenuItem());
			fileMenu.add(getSaveMenuItem());
			fileMenu.add(getImportMenuItem());
			fileMenu.add(getExportMenuItem());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes EditionMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	protected JMenu getEditionMenu() {
		if (EditionMenu == null) {
			EditionMenu = new JMenu();
			EditionMenu.setText("Edition");
			EditionMenu.add(getCopyMenuItem());
			EditionMenu.add(getPasteMenuItem());
		}
		return EditionMenu;
	}

	/**
	 * This method initializes openMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	protected JMenuItem getOpenMenuItem() {
		if (openMenuItem == null) {
			openMenuItem = new JMenuItem();
			openMenuItem.setText("Open...");
			openMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser chooser = new JFileChooser();
					FileFilter filter = new WorldFileFilter();
					chooser.setFileFilter(filter);
					int returnVal = chooser
							.showOpenDialog(GenericMapEditor.this);
					if (returnVal == JFileChooser.APPROVE_OPTION) {
						jScrollPane.setVisible(true);
						GenericMapEditor.filename = chooser.getSelectedFile()
								.getAbsolutePath();
						saveMenuItem.setEnabled(true);
						System.out.println("You chose to open this file: "
								+ GenericMapEditor.filename);
						loadWorld(filename);
						saveMenuItem.setEnabled(true);
						exportMenuItem.setEnabled(true);
					}

				}
			});
		}
		return openMenuItem;
	}

	/**
	 * This method initializes SaveMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	protected JMenuItem getSaveMenuItem() {
		if (saveMenuItem == null) {
			saveMenuItem = new JMenuItem();
			saveMenuItem.setText("Save");
			saveMenuItem.setEnabled(false);
			saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					saveWorld();
				}
			});
		}
		return saveMenuItem;
	}

	protected JMenuItem getExportMenuItem() {
		if (exportMenuItem == null) {
			exportMenuItem = new JMenuItem();
			exportMenuItem.setText("Export");
			exportMenuItem.setEnabled(false);
			exportMenuItem
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							// saveWorld();
							// OptimizedSaver.saveWorld("C:/out.txt");
							new Exporter(getInstance()).setVisible(true);
						}
					});
		}
		return exportMenuItem;
	}

	protected JMenuItem getImportMenuItem() {
		if (importMenuItem == null) {
			importMenuItem = new JMenuItem();
			importMenuItem.setText("Import");
			importMenuItem.setEnabled(true);
			importMenuItem
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							JFileChooser chooser = new JFileChooser();
							// FileFilter filter = new WorldFileFilter();
							// chooser.setFileFilter(filter);
							int returnVal = chooser
									.showOpenDialog(GenericMapEditor.this);
							if (returnVal == JFileChooser.APPROVE_OPTION) {
								jScrollPane.setVisible(true);
								GenericMapEditor.filename = chooser
										.getSelectedFile().getAbsolutePath();
								saveMenuItem.setEnabled(true);
								System.out
										.println("You chose to open this file: "
												+ GenericMapEditor.filename);
								OptimizedSaver.loadWorld(filename);
								saveMenuItem.setEnabled(true);
								exportMenuItem.setEnabled(true);
							} else {
								System.out.println("cancel");
							}
						}
					});
		}
		return importMenuItem;
	}

	/**
	 * This method initializes exitMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	protected JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					close();
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	protected void close() {
		System.out.println("Window closing");
		if (!filename.equals("")) {
			int n = JOptionPane.showConfirmDialog(getInstance(),
					"Would you like to save world before leaving?",
					"Saving world...", JOptionPane.YES_NO_OPTION);
			if (n == JOptionPane.YES_OPTION) {
				System.out.println("saving world");
				saveWorld();
			}
		}
		// Saving texture config
		saveTextureButton();
	}

	/**
	 * This method initializes newMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	protected JMenuItem getNewMenuItem() {
		if (newMenuItem == null) {
			newMenuItem = new JMenuItem();
			newMenuItem.setText("New World");
			newMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					NewWorldDialog diag = new NewWorldDialog(getInstance());
					diag.setVisible(true);
					if (diag.pressedOk) {
						graphicBoard.changeDimension(diag.worldWidth,
								diag.worldHeight, diag.tileWidth,
								diag.tileHeight);
						filename = diag.filename;
						getInstance().setTitle("Tiles Map Editor " + filename);
						jScrollPane.setVisible(true);
						saveMenuItem.setEnabled(true);
						exportMenuItem.setEnabled(true);
					}
				}
			});
		}
		return newMenuItem;
	}

	public GenericMapEditor getInstance() {
		return this;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */

	protected JPanel getJPanel() {
		if (jPanel == null) {
			selectedTile = new JLabel();
			selectedTile.setBounds(new Rectangle(711, 17, 141, 32));
			selectedTile.setText("Selected texture or tool");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setPreferredSize(new Dimension(700, 500));
			jPanel.add(getJScrollPane(), null);
			jPanel.add(selectedTile, null);
			jPanel.add(getJScrollPaneTexture(), null);
			jPanel.add(getZoomIn(), null);
			jPanel.add(getZoomOut(), null);
			jPanel.add(getSelectCursor(), null);
			jPanel.add(getSelectPaste(), null);
			jPanel.add(getCurrentJPanel(), null);
			jPanel.add(getSelectBlank(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes graphicBoard
	 * 
	 * @return generic.GraphicBoard
	 */
	protected GraphicBoard getGraphicBoard() {
		if (graphicBoard == null) {
			graphicBoard = new GraphicBoard(20, 20, 64, 64);
			graphicBoard.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mousePressed(java.awt.event.MouseEvent e) {
					if (isTool) {
						CopierTool.onClick(e.getX(), e.getY());
					} else {
						graphicBoard.setTile(e.getX(), e.getY(),
								currentJPanel.cimg);
						// new CustomImage(currentJPanel.cimg));
					}
				}
			});

		}
		return graphicBoard;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	protected JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setVisible(false);
			jScrollPane.setPreferredSize(new Dimension(700, 600));
			jScrollPane
					.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
			jScrollPane
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
			jScrollPane.setLocation(new Point(5, 5));
			jScrollPane.setSize(new Dimension(700, 600));
			jScrollPane.setViewportView(getGraphicBoard());
			(ZoomManager.board = getGraphicBoard()).setScroll(jScrollPane);

		}
		return jScrollPane;
	}

	/**
	 * This method initializes editorMenu
	 * 
	 * @return javax.swing.JMenu
	 */
	protected JMenu getEditorMenu() {
		if (editorMenu == null) {
			editorMenu = new JMenu();
			editorMenu.setText("Tiles");
			editorMenu.add(getAddTextureMenuItem());
			editorMenu.add(getAddAllTextureMenuItem());
			editorMenu.add(getRemoveAllTextureMenuItem());
		}
		return editorMenu;
	}

	/**
	 * This method initializes addTextureMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	protected JMenuItem getAddTextureMenuItem() {
		if (addTextureMenuItem == null) {
			addTextureMenuItem = new JMenuItem();
			addTextureMenuItem.setText("Add texture...");
			addTextureMenuItem
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							String texture_filename = "";
							JFileChooser chooser = new JFileChooser();
							FileFilter filter = new ImageFileFilter();
							chooser.setFileFilter(filter);
							int returnVal = chooser
									.showOpenDialog(GenericMapEditor.this);
							if (returnVal == JFileChooser.APPROVE_OPTION) {
								texture_filename = chooser.getSelectedFile()
										.getAbsolutePath();
								saveMenuItem.setEnabled(true);
								System.out
										.println("You chose to open this texture: "
												+ texture_filename);
							}

							TextureButton tb = new TextureButton(
									texture_filename);

							textureArea.add(tb);
							textureArea.revalidate();
							textureArea.repaint();
						}
					});
		}
		return addTextureMenuItem;
	}

	/**
	 * This method initializes addAllTextureMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	protected JMenuItem getAddAllTextureMenuItem() {
		if (addAllTextureMenuItem == null) {
			addAllTextureMenuItem = new JMenuItem();
			addAllTextureMenuItem
					.setText("Add all textures from a directory...");
			addAllTextureMenuItem
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							String dirname = "";
							JFileChooser chooser = new JFileChooser();

							chooser
									.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

							int returnVal = chooser
									.showOpenDialog(GenericMapEditor.this);
							if (returnVal == JFileChooser.APPROVE_OPTION) {
								FilenameFilter filter = new ImageFileFilter();
								File dir = chooser.getSelectedFile();
								dirname = dir.getAbsolutePath();

								File[] files = dir.listFiles(filter);
								for (int i = 0; i < files.length; ++i) {
									TextureButton tb = new TextureButton(
											files[i].getAbsolutePath());
									textureArea.add(tb);
								}
								saveMenuItem.setEnabled(true);
								System.out
										.println("You chose to open this directory: "
												+ dirname);
							}

							textureArea.revalidate();
							textureArea.doLayout();
							textureArea.repaint();
						}
					});
		}
		return addAllTextureMenuItem;
	}

	/**
	 * This method initializes RemoveAllTextureMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	protected JMenuItem getRemoveAllTextureMenuItem() {
		if (RemoveAllTextureMenuItem == null) {
			RemoveAllTextureMenuItem = new JMenuItem();
			RemoveAllTextureMenuItem.setText("Remove all textures");
			RemoveAllTextureMenuItem
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							textureArea.removeAll();
							textureArea.validate();
							textureArea.repaint();
						}
					});
		}
		return RemoveAllTextureMenuItem;
	}

	/**
	 * This method initializes jScrollPaneTexture
	 * 
	 * @return javax.swing.JScrollPane
	 */
	protected JScrollPane getJScrollPaneTexture() {
		if (jScrollPaneTexture == null) {
			jScrollPaneTexture = new JScrollPane();
			jScrollPaneTexture.setBounds(new Rectangle(712, 194, 251, 412));
			jScrollPaneTexture
					.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
			jScrollPaneTexture
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			jScrollPaneTexture.setPreferredSize(new Dimension(251, 509));
			jScrollPaneTexture.setViewportView(getTextureArea());
		}
		return jScrollPaneTexture;
	}

	/**
	 * This method initializes textureArea
	 * 
	 * @return javax.swing.JPanel
	 */
	protected JPanel getTextureArea() {
		if (textureArea == null) {
			textureArea = new JPanel();
			textureArea.setPreferredSize(new Dimension(220, 5000));
			textureArea.setSize(new Dimension(220, 5000));
			OptimizedSaver.textureArea = textureArea;
		}
		return textureArea;
	}

	/**
	 * This method initializes zoomIn
	 * 
	 * @return javax.swing.JButton
	 */
	protected JButton getZoomIn() {
		if (zoomIn == null) {
			zoomIn = new JButton();
			zoomIn.setBounds(new Rectangle(11, 610, 101, 27));
			zoomIn.setText("Zoom In +");
			zoomIn.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					// System.out.println("Zoom_out actionPerformed()");
					ZoomManager.decZoomFactor();
				}
			});
		}
		return zoomIn;
	}

	/**
	 * This method initializes ZoomOut
	 * 
	 * @return javax.swing.JButton
	 */
	protected JButton getZoomOut() {
		if (ZoomOut == null) {
			ZoomOut = new JButton();
			ZoomOut.setBounds(new Rectangle(123, 610, 110, 27));
			ZoomOut.setText("Zoom Out -");
			ZoomOut.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					// System.out.println("Zoom_out actionPerformed()");
					ZoomManager.incZoomFactor();
				}
			});
		}
		return ZoomOut;
	}

	/**
	 * This method initializes copyMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	protected JMenuItem getCopyMenuItem() {
		if (copyMenuItem == null) {
			copyMenuItem = new JMenuItem();
			copyMenuItem.setText("Copy");
			copyMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO
					// Auto-generated
					// Event stub
					// actionPerformed()
				}
			});
		}
		return copyMenuItem;
	}

	/**
	 * This method initializes pasteMenuItem
	 * 
	 * @return javax.swing.JMenuItem
	 */
	protected JMenuItem getPasteMenuItem() {
		if (pasteMenuItem == null) {
			pasteMenuItem = new JMenuItem();
			pasteMenuItem.setText("Paste");
			pasteMenuItem
					.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							System.out.println("actionPerformed()"); // TODO
							// Auto-generated
							// Event
							// stub
							// actionPerformed()
						}
					});
		}
		return pasteMenuItem;
	}

	/**
	 * This method initializes selectCursor
	 * 
	 * @return javax.swing.JButton
	 */
	protected JButton getSelectCursor() {
		if (selectCursor == null) {
			selectCursor = new JButton();
			selectCursor.setBounds(new Rectangle(712, 93, 152, 27));
			selectCursor.setText("Select copier");
			selectCursor.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setToolCopier();
				}
			});
		}
		return selectCursor;
	}

	/**
	 * This method initializes selectPaste
	 * 
	 * @return javax.swing.JButton
	 */
	protected JButton getSelectPaste() {
		if (selectPaste == null) {
			selectPaste = new JButton();
			selectPaste.setBounds(new Rectangle(712, 125, 153, 25));
			selectPaste.setText("Select paster");
			selectPaste.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					setToolPaster();
				}
			});
		}
		return selectPaste;
	}

	public static void setToolCopier() {
		CopierTool.copier = true;
		CopierTool.startPos = true;
		currentJPanel.image = image_copier;
		isTool = true;
		currentJPanel.repaint();
	}

	public static void setToolPaster() {
		CopierTool.copier = false;
		CopierTool.startPos = true;
		currentJPanel.image = image_paster;
		isTool = true;
		currentJPanel.repaint();
	}

	public void saveTextureButton() {
		FileOutputStream outStream; // generic stream to the file
		ObjectOutputStream objStream; // stream for objects to the file
		try {
			// setup a stream to a physical file on the filesystem
			outStream = new FileOutputStream(configFilename);

			// attach a stream capable of writing objects to the stream that is
			// connected to the file
			objStream = new ObjectOutputStream(outStream);

			objStream.writeInt(textureArea.getComponentCount());

			Component comps[] = textureArea.getComponents();

			for (int i = 0; i < comps.length; ++i) {
				TextureButton tb = ((TextureButton) comps[i]);

				tb.img_pixels = tb.getArrayFromImage(tb.img);
				objStream.writeObject(tb);
			}
			objStream.close();
			outStream.close();
		} catch (IOException e) {
			System.err.println("Things not going as planned.");
			e.printStackTrace();
		} // catch
	}

	public void loadTextureButton() {
		FileInputStream inStream; // generic stream to the file
		ObjectInputStream objStream; // stream for objects to the file
		try {
			// setup a stream to a physical file on the filesystem
			inStream = new FileInputStream(configFilename);

			// attach a stream capable of writing objects to the stream that is
			// connected to the file
			objStream = new ObjectInputStream(inStream);

			int nbComps = objStream.readInt();

			for (int i = 0; i < nbComps; ++i) {
				TextureButton tb = (TextureButton) objStream.readObject();
				tb.setUpFromSerialization();
				textureArea.add(tb);
			}
			textureArea.validate();
			textureArea.repaint();

			objStream.close();
			inStream.close();
		} catch (IOException e) {
			System.err.println("IOException");
			e.printStackTrace();
		} catch (Exception e) {
			System.err.println("Things not going as planned.");
			e.printStackTrace();
		}

	}

	public void saveWorld() {
		FileOutputStream outStream; // generic stream to the file
		ObjectOutputStream objStream; // stream for objects to the file
		try {
			// setup a stream to a physical file on the filesystem
			outStream = new FileOutputStream(filename);

			// attach a stream capable of writing objects to the stream that is
			// connected to the file
			objStream = new ObjectOutputStream(outStream);

			// World parameters
			objStream.writeInt(GraphicBoard.worldWidth);
			objStream.writeInt(GraphicBoard.worldHeight);
			objStream.writeInt(GraphicBoard.tileWidth);
			objStream.writeInt(GraphicBoard.tileHeight);

			// Save the filename placed in world coordinates
			for (int i = 0; i < GraphicBoard.worldWidth; ++i)
				for (int j = 0; j < GraphicBoard.worldHeight; ++j) {
					if (GraphicBoard.imageGrid[i][j] != null) {
						objStream
								.writeObject(GraphicBoard.imageGrid[i][j].filename);
						objStream
								.writeObject(GraphicBoard.imageGrid[i][j].type);
					} else {
						objStream.writeObject("");
						objStream.writeObject("");
					}
				}

			objStream.close();
			outStream.close();
			System.out.println("Successfully saved at " + filename);
		} catch (IOException e) {
			System.err.println("Things not going as planned.");
			e.printStackTrace();
		} // catch
	}

	public void loadWorld(String file) {
		FileInputStream inStream; // generic stream to the file
		ObjectInputStream objStream; // stream for objects to the file
		try {
			// setup a stream to a physical file on the filesystem
			inStream = new FileInputStream(file);

			// attach a stream capable of writing objects to the stream that is
			// connected to the file
			objStream = new ObjectInputStream(inStream);

			// World parameters

			int worldWidth = objStream.readInt();
			int worldHeight = objStream.readInt();
			int tileWidth = objStream.readInt();
			int tileHeight = objStream.readInt();

			graphicBoard.changeDimension(worldWidth, worldHeight, tileWidth,
					tileHeight);

			String name, type;
			// Save the filename placed in world coordinates
			for (int i = 0; i < GraphicBoard.worldWidth; ++i)
				for (int j = 0; j < GraphicBoard.worldHeight; ++j) {
					name = (String) objStream.readObject();
					type = (String) objStream.readObject();
					if (name.equals(""))
						GraphicBoard.imageGrid[i][j] = null;
					else {
						GraphicBoard.imageGrid[i][j] = new CustomImage(name);
						GraphicBoard.imageGrid[i][j].type = type;
					}
				}

			objStream.close();
			inStream.close();
			System.out.println("Successfully loaded from " + file);
		} catch (IOException e) {
			System.err.println("Things not going as planned.");
			e.printStackTrace();
		} catch (Exception e) {
			e.getMessage();
		} // catch

	}

	/**
	 * This method initializes currentJPanel
	 * 
	 * @return org.easyway.tiles.generic.CurrentJPanel
	 */
	protected CurrentJPanel getCurrentJPanel() {
		if (currentJPanel == null) {

			currentJPanel = new CurrentJPanel(new CustomImage(getClass()
					.getResource("images/copier.JPG").getPath()));
			MediaTracker mt = new MediaTracker(currentJPanel);
			image_copier = Toolkit.getDefaultToolkit().getImage(
					getClass().getResource("images/copier.JPG"));
			image_paster = Toolkit.getDefaultToolkit().getImage(
					getClass().getResource("images/paster.JPG"));
			mt.addImage(image_copier, 0);
			mt.addImage(image_paster, 1);
			try {
				mt.waitForAll();
			} catch (InterruptedException e) {
				System.out.println("images loaded");
			}
		}
		return currentJPanel;
	}

	/**
	 * This method initializes selectBlank
	 * 
	 * @return javax.swing.JButton
	 */
	protected JButton getSelectBlank() {
		if (selectBlank == null) {
			selectBlank = new JButton();
			selectBlank.setBounds(new Rectangle(712, 154, 153, 26));
			selectBlank.setText("Select blank");
			selectBlank.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					currentJPanel.image = null;// new
					// BufferedImage(1,1,BufferedImage.TYPE_3BYTE_BGR);
					currentJPanel.cimg = null;
					// currentJPanel.cimg.filename = "";
					isTool = false;
					currentJPanel.repaint();
				}
			});
		}
		return selectBlank;
	}

} // @jve:decl-index=0:visual-constraint="10,10"
